package com.marvels.product.param.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dao.McptConsultingFeeMapper;
import com.marvels.product.param.dto.McptConsultingFeeDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.ConsultingFeeService;

@Service
public class ConsultingFeeServiceImpl implements ConsultingFeeService {

	@Autowired						
	private McptConsultingFeeMapper mcptConsultingFeeMapper;
	
	@Override
	public PageInfo<McptConsultingFeeDTO> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
		
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptConsultingFeeDTO>  mcptConsultingFeeList = mcptConsultingFeeMapper.getListByParam(param);
		
		PageInfo<McptConsultingFeeDTO> pageInfo = new PageInfo<McptConsultingFeeDTO>(mcptConsultingFeeList);
	    
		return pageInfo;
	}

	@Override
	public McptConsultingFeeDTO getEntityByPrimaryKey(Long consultingFeeId) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptConsultingFeeMapper.selectByPrimaryKey(consultingFeeId);
	}

	@Override
	public int insert(McptConsultingFeeDTO dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptConsultingFeeMapper.insert(dto);
	}

	@Override
	public int updateByPrimaryKey(McptConsultingFeeDTO dto) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		return mcptConsultingFeeMapper.updateByPrimaryKey(dto);
	}

	@Override
	public int updateStatus(McptConsultingFeeDTO dto) throws MarvelsBusinessException{
		Integer count = 0;
		try {
			 count  = mcptConsultingFeeMapper.updateStatus(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>咨询费审核异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}

	@Override
	public McptConsultingFeeDTO getConsultingFeeByConsultingFeeCode(McptProduct dto) throws MarvelsBusinessException {
		return mcptConsultingFeeMapper.selectConsultingFeeByConsultingFeeCode(dto);
	}

	@Override
	public McptConsultingFeeDTO selectByProductInfo(McptProduct product) {
		return mcptConsultingFeeMapper.selectByProductInfo(product);
	}

	public int deleteByPrimaryKey(McptConsultingFeeDTO record) throws MarvelsBusinessException {
		Integer count = 0;
		try {
			 record.setDeleteFlag(Byte.valueOf("1"));
			 count  = mcptConsultingFeeMapper.deleteByPrimaryKey(assemblyParam(record));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>咨询费删除异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}
	/**
	 * 组装参数
	 * @param record
	 * @return
	 * @throws Exception
	 */
	private McptConsultingFeeDTO assemblyParam(McptConsultingFeeDTO record) throws Exception{
		String userName = MarvelsConstant.SYSTEM_USER;
		 UserDTO user = CacheUtils.getCacheUser();
		 if(user!=null){
			 userName = user.getUserName();
		 }
		 record.setUpdatedBy(userName);
		 return record;
	}

}
